package fr.umlv.m2.jee.spij.service.lesson;

import fr.umlv.m2.jee.spij.persistence.education.Lesson;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Date;

/**
 * DTO that displays datas about {@link Lesson}.
 *
 * @author thomas magny
 * @author mathieu mazzolini
 */
@Component("lessonBean")
public class DisplayLesson implements Serializable {

    /**
     * Primary key.
     */
    private long id;

    /**
     * Id of linked unit.
     */
    private Long unit;

    /**
     * Name of linked unit.
     */
    private String unitName;

    /**
     * About teaching.
     */
    private String subject;

    /**
     * Contain of lesson.
     */
    private String contain;

    /**
     * Link of lesson in PDF.
     */
    private String linkSupport;

    /**
     * Instructional date.
     */
    private Date date;

    /**
     * Instructional start time.
     */
    private Date startTime;

    /**
     * Instructional end time.
     */
    private Date endTime;

    /**
     * Default constructor
     */
    public DisplayLesson() {
        unit = -1l;
        id = -1l;
    }

    /**
     * Constructors that store lesson datas
     *
     * @param lesson the object to transfer datas
     */
    public DisplayLesson(Lesson lesson) {
        if (lesson != null)
            storeData(lesson);
    }

    /**
     * @return the primary key
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the unit id
     */
    public Long getUnit() {
        return unit;
    }

    /**
     * @param unit the unit to set
     */
    public void setUnit(Long unit) {
        this.unit = unit;
    }

    /**
     * @return the subjet
     */
    public String getSubject() {
        return subject;
    }

    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    /**
     * @return the unit contain
     */
    public String getContain() {
        return contain;
    }

    /**
     * @param contain the contain to set
     */
    public void setContain(String contain) {
        this.contain = contain;
    }

    /**
     * @return the link support
     */
    public String getLinkSupport() {
        return linkSupport;
    }

    /**
     * @param linkSupport the link support to set
     */
    public void setLinkSupport(String linkSupport) {
        this.linkSupport = linkSupport;
    }

    /**
     * @return the unitName
     */
    public String getUnitName() {
        return unitName;
    }

    /**
     * @param unitName the unitName to set
     */
    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

    /**
     * @return the date
     */
    public Date getDate() {
        return new Date(date.getTime());
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.date = new Date(date.getTime());
    }

    /**
     * @return the startTime
     */
    public Date getStartTime() {
        return new Date(startTime.getTime());
    }

    /**
     * @param startTime the startTime to set
     */
    public void setStartTime(Date startTime) {
        this.startTime = new Date(startTime.getTime());
    }

    /**
     * @return the endTime
     */
    public Date getEndTime() {
        return new Date(endTime.getTime());
    }

    /**
     * @param endTime the endTime to set
     */
    public void setEndTime(Date endTime) {
        this.endTime = new Date(endTime.getTime());
    }

    private void storeData(Lesson lesson) {
        if (lesson.getId() > 0)
            setId(lesson.getId());
        if (lesson.getUnit() > 0)
            setUnit(lesson.getUnit());
        if (!lesson.getContain().isEmpty())
            setContain(lesson.getContain());
        if (lesson.getDuration() != null) {
            setDate(lesson.getDuration().getStart().toDate());
            setStartTime(lesson.getDuration().getStart().toDate());
            setEndTime(lesson.getDuration().getEnd().toDate());
        }
        if (!lesson.getLinkSupport().isEmpty())
            setLinkSupport(lesson.getLinkSupport());
        if (!lesson.getSubject().isEmpty())
            setSubject(lesson.getSubject());
    }
}
